<?php
class crash_report {
    public $crash_time;
    public $build_time;
    public $process;
    public $pid;
    public $crash_report_path;
    public $backtrace;
    public $crash_report;
    public $variant;
    public $sw_version;
    public $reporter;
    public $upload_time;
    public $process_type;

    public function __get($property_name) {
        if (isset($this->$property_name)) {
            return ($this->$property_name);
        } else {
            return (NULL);
        }
    }

    public function __set($property_name, $value) {
        $this->$property_name = $value;
    }
    protected function _find_libsys() {
        $filter = "*".$this->build_time."*".$this->variant;
        $daily_paths = CONFIG::get("daily_paths");
        foreach($daily_paths as $path) {
            $command = "find ".$path." -type d -a -name '".$filter."'";
            exec($command, $data);
            if (count($data) != 0) {
                return $data[0];
            }
        }
        return NULL;
    }
    public function get_project() {
        if (preg_match( "/(.*_[\d])/", $this->variant, $matches )) {
            return $matches[0];
        }
    }
    public function build_backtrace() {
        if ($this->process_type == PROCESS_TYPE_NATIVE) {
            if (strstr($this->process, "hydra") != FALSE) {
                $temp_file_name = tempnam(sys_get_temp_dir(), "crash_report");
                $build_dir = $this->_find_libsys();
                $handle = fopen($temp_file_name, "w");
                fwrite($handle, $this->crash_report);
                fclose($handle);
                $ndk_stack_path = path_join(NDK_PATH, "ndk-stack");
                $libsys_path = path_join($build_dir, "log");
                $command = $ndk_stack_path." -sym ".$libsys_path." -dump ".$temp_file_name;
                # Get the output
                exec($command, $stack_array);
                $stack_dump = implode("\n", $stack_array);
                $this->backtrace = $stack_dump;
                unlink($temp_file_name);
            } else {
                $this->backtrace = $this->crash_report;
            }
        } else if ($this->process_type == PROCESS_TYPE_JAVA) {
            $this->backtrace = $this->crash_report;
        }
    }
}
